<?php namespace Home\Controller;
use Hdphp\Controller\Controller;
/*
 * 列表控制器
 * */
	 class ListController extends CommonController
	 {
	 	public function index()
		{
			//获得get参数
			$cid=Q("cid",0,"intval");
			$tid=Q("tid",0,"inyval");
			
			//获得自己和子集下的文章
			//1.实例化category控制器 使用getSon方法  获得子类
			$listdata = new \Common\Model\Category;
			//通过类进入页面
			if($cid){
				$cids = $listdata->getSon($listdata->get(),$cid);
				//将自己压入数组
				$cids[] = $cid;
				//查找数据
				$listheader = array(
					"cate"=>"分类",
					"catename"=>Db::table("category")->where("cid",$cid)->pluck("cname"),
					"count"=>Db::table("article")->where("category_cid in (" . implode(",", $cids) . ")")->count(),
				);
				//p($listheader);die;
				View::with("listheader",$listheader);
				//获得所有文章shuju 
				$articledata = Db::table("article")
							 ->join("category","cid","=","category_cid")
							 ->where("category_cid in (" . implode(",", $cids) . ")")
							 ->where("is_recycle",0)
							 ->get();
				foreach($articledata as $key=>$value){
					$articledata[$key]["total"] = Db::table("tag")
										  ->join("article_tag","tid","=","tag_tid")
										  ->where("article_aid",$value["aid"])
										  ->lists("tid,tname");
				}
				//p($articledata);die;
				View::with("articledata",$articledata);
			}
			//p($listSon);die;
			/*
			 * 
			 * 通过标签连接进入
			 * */
			if($tid){
				//获得头部数据
				$listheader = array(
					"cate"=>"标签",
					"catename"=>Db::table("tag")->where("tid",$tid)->pluck("tname"),
					"count"=>Db::table("article")->join("article_tag","aid","=","article_aid")->where("tag_tid",$tid)->count(),
				);
				//p($listheader);die;
				View::with("listheader",$listheader);
				$aids = Db::table("article_tag")->where("tag_tid",$tid)->lists("article_aid");
				//p($aids);die;
				if($aids){
					$articledata = Db::table("article")
					->join("category","cid","=","category_cid")
					->where("aid in(".implode(",", $aids).")")
					->where("is_recycle",0)
					->get();
					foreach($articledata as $k=>$v){
						$articledata[$k]["total"] = Db::table("tag")
						->join("article_tag","tag_tid","=","tid")
						->where("article_aid",$v["aid"])
						->lists("tid,tname");
					}
				}else{
					$articledata = array();
				}
				//p($articledata);die;
				View::with("articledata",$articledata);
			}
			
			View::make("list");
		}
	 }